<template>
  <div class="app-container">
    <div class="filter-container">
      <SearchOptionClass :class-id="listQuery.class_id" @setSearchClassId="searchClassId" />
      <div class="filter-item">
        <el-date-picker
          v-model="listQuery.stime"
          value-format="yyyy-MM-dd HH:mm:ss"
          type="datetimerange"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        />
      </div>
      <el-button v-waves class="filter-item" type="primary" icon="el-icon-search" @click="handleFilter">
        查询
      </el-button>
      <el-button class="filter-item" style="margin-left: 10px;" type="primary" icon="el-icon-edit" @click="handleCreate">
        添加
      </el-button>
    </div>

    <el-table
      :key="tableKey"
      v-loading="listLoading"
      :header-cell-style="{background:'#ccc',color:'black'}"
      :data="list"
      border
      stripe
      fit
      highlight-current-row
      style="width: 100%;"
    >
      <el-table-column label="ID" align="center" width="80">
        <template slot-scope="{row}">
          <span>{{ row.id }}</span>
        </template>
      </el-table-column>
      <el-table-column label="班级" align="center">
        <template slot-scope="{row}">
          <span>{{ row.classInfo.name }}</span>
        </template>
      </el-table-column>
      <el-table-column label="课程图片大" align="center" width="100">
        <template slot-scope="{row}">
          <img :src="returnImageUrl(row.img_big)" alt="" class="avatar" style="width: 50px;height: 50px">
        </template>
      </el-table-column>
      <el-table-column label="课程图片小" align="center" width="100">
        <template slot-scope="{row}">
          <img :src="returnImageUrl(row.img_small)" alt="" class="avatar" style="width: 50px;height: 50px">
        </template>
      </el-table-column>
      <el-table-column label="APP显示" align="center" width="80">
        <template slot-scope="{row}">
          <span>{{ isShowOptions[row.is_show] }}</span>
        </template>
      </el-table-column>

      <el-table-column label="选修课开放时间" align="center">
        <template slot-scope="{row}">
          <span>{{ row.stime }}</span>
        </template>
      </el-table-column>

      <el-table-column label="选修课结束时间" align="center">
        <template slot-scope="{row}">
          <span>{{ row.etime }}</span>
        </template>
      </el-table-column>

      <el-table-column label="操作" align="center" width="230" class-name="small-padding fixed-width">
        <template slot-scope="{row,$index}">
          <el-button type="primary" size="mini" @click="handleUpdate(row)">
            编辑
          </el-button>
          <el-button type="danger" size="mini" @click="handleDelete(row,$index)">
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.limit" @pagination="getList" />

    <el-dialog :title="textMap[dialogStatus]" :visible.sync="dialogFormVisible">
      <el-form ref="dataForm" :rules="rules" :model="temp" label-position="left" label-width="110px" style="width: 400px; margin-left:50px;">
        <el-form-item v-model="temp.class_id" label="班级" prop="class_id" class="filter-item">
          <SearchOptionClass :class-id="temp.class_id" @setSearchClassId="updateClassId" />
        </el-form-item>
        <el-form-item label="课程图片大" prop="img_big" height="50px">
          <ImageUpload :upload-name="uploadName" :upload-image-url="returnImageUrl(temp.img_big)" @getReturnImageUrl="setReturnImageBigUrl" />
        </el-form-item>
        <el-form-item label="课程图片小" prop="img_small" height="50px">
          <ImageUpload :upload-name="uploadName" :upload-image-url="returnImageUrl(temp.img_small)" @getReturnImageUrl="setReturnImageSmallUrl" />
        </el-form-item>
        <el-form-item label="是否App显示">
          <el-select v-model="temp.is_show" prop="is_show" class="filter-item">
            <el-option v-for="(value,key) in isShowOptions" :key="key" :label="value" :value="key" />
          </el-select>
        </el-form-item>
        <el-form-item label="开始日期" prop="stime">
          <div class="block">
            <el-date-picker
              v-model="temp.stime"
              type="date"
              value-format="yyyy-MM-dd"
              placeholder="开始日期"
            />
          </div>
        </el-form-item>
        <el-form-item label="结束日期" prop="etime">
          <div class="block">
            <el-date-picker
              v-model="temp.etime"
              type="date"
              value-format="yyyy-MM-dd"
              placeholder="结束日期"
            />
          </div>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">
          取消
        </el-button>
        <el-button type="primary" @click="dialogStatus==='create'?createData():updateData()">
          确定
        </el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { classTimePage, classTimeUpdate, classTimeDelete } from '@/api/xuanxiu'
import waves from '@/directive/waves' // waves directive
import { isInteger } from '@/utils/validate'
import { notifySuccess, my_confirm, listQueryFilter, getFileUrl } from '@/utils/tool'
import SearchOptionClass from '@/components/SearchOption/class'
import Pagination from '@/components/Pagination'
import ImageUpload from '@/components/Upload/ImageUpload'

export default {
  name: 'XuanxiuClassTime',
  components: { Pagination, SearchOptionClass, ImageUpload },
  directives: { waves },
  data() {
    return {
      tableKey: 0,
      list: null,
      total: 0,
      listLoading: true,
      uploadName: 'UpLoadImage',
      listQuery: {
        page: 1,
        limit: 20,
        class_id: null,
        stime: '',
        querySort: '-id'
      },
      isShowOptions: {
        0: '不显示',
        1: '显示'
      },
      temp: {
        id: null,
        class_id: null,
        img_small: '',
        img_big: '',
        is_show: 1,
        stime: '',
        etime: ''
      },
      dialogFormVisible: false,
      dialogStatus: '',
      textMap: {
        update: '编辑',
        create: '添加'
      },
      rules: {
        name: [{ required: true, message: '名称必填', trigger: 'change' }],
        address: [{ required: true, message: '地址必填', trigger: 'change' }],
        sort: [{ required: true, message: '排序必填', trigger: 'change' }, { validator: isInteger, trigger: 'blur' }]
      },
      downloadLoading: false
    }
  },
  created() {
    this.getList()
  },
  methods: {
    getList() {
      this.listLoading = true
      this.listQuery = listQueryFilter(this.listQuery)
      classTimePage(this.listQuery).then(response => {
        this.list = response.data
        this.total = response.page
        this.listLoading = false
      })
    },
    handleFilter() {
      this.listQuery.page = 1
      this.getList()
    },
    resetTemp() {
      this.temp = {
        class_id: null,
        img_small: '',
        img_big: '',
        is_show: 1,
        stime: '',
        etime: ''
      }
    },
    handleCreate() {
      this.resetTemp()
      this.dialogStatus = 'create'
      this.dialogFormVisible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    createData() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          classTimeUpdate(this.temp).then(response => {
            var rs = response.data
            this.list.unshift(rs)
            this.dialogFormVisible = false
            notifySuccess(this, '创建成功')
          })
        }
      })
    },
    handleUpdate(row) {
      this.temp = Object.assign({}, row)
      this.dialogStatus = 'update'
      this.dialogFormVisible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    updateData() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          const tempData = Object.assign({}, this.temp)
          classTimeUpdate(tempData).then(response => {
            var rs = response.data
            const index = this.list.findIndex(v => v.id === rs.id)
            this.list.splice(index, 1, rs)
            this.dialogFormVisible = false
            notifySuccess(this, '修改成功')
          })
        }
      })
    },
    handleDelete(row, index) {
      my_confirm(this, () => {
        classTimeDelete({ id: row.id }).then(() => {
          this.list.splice(index, 1)
          this.dialogFormVisible = false
          notifySuccess(this, '删除成功')
        })
      })
    },
    returnImageUrl(photo) {
      return getFileUrl(photo)
    },
    setReturnImageBigUrl: function(data) {
      this.temp.img_big = data
      notifySuccess(this, '图片上传成功,请点击确定修改')
    },
    setReturnImageSmallUrl: function(data) {
      this.temp.img_small = data
      notifySuccess(this, '图片上传成功,请点击确定修改')
    },
    searchClassId: function(data) {
      this.listQuery.class_id = data.classId
    },
    updateClassId: function(data) {
      this.temp.class_id = data.classId
    }
  }
}
</script>
